import { defineStore } from "pinia";
import { ref } from "vue";
import { passwordLogin, codeLogin } from "@/api/login";
import { selectByToken, updateUserInfo, getSchools, getmajors, getProvinces, selectEmployByToken, getJobNames } from "@/api/userInformation";
import * as commonUtil from "@/utils/commonUtil";
import { ElMessage } from "element-plus";

export const useUserInfor = defineStore("userInfor", {
  state: () => {
    return {
      userInfo: {},
      role: '',
      schoolList: [],//学院 
      majorList: [],//专业
      provinceList: [],//地区列表
      jobList: [],//职位列表
      nav: 0,
      activeName: 'one',
      token:''
    };
  },
  actions: {
    //登录
    async Login(from, router, isCodeLogin, rememberPassword, isLoginLoading) {
      const res = ref();
      if (!isCodeLogin.value) {
        //密码登录
        res.value = await passwordLogin(from);
      } else {
        //验证码登录
        res.value = await codeLogin(from);
      }
      if (res.value.code === 200) {//登录成功
        this.token = res.value.data.token
        ElMessage({//提示登录成功
          type: "success",
          message: res.value.msg,
        });


        //是否启用记住我
        if (rememberPassword) {
          //是否长期存储token
          commonUtil.setStoreByOneDate("token", res.value.data.token);
        } else {
          //将token存入cookie中
          commonUtil.setStore("token", res.value.data.token);
        }
        ElMessage({
          type: "success",
          message: "即将前往首页",
        });

        //根据用户角色判断导航栏的显示内容
        if (from.role === 'EMPLOY') {
          this.nav = 1
          this.role = 'EMPLOY'
          this.queryEmployInfo();//查询企业用户信息
          router.push("/employHome");
        }
        if (from.role === 'USER') {
          this.nav = 0
          this.role = 'USER'
          this.queryUserInfo();//查询学生用户信息
          //路由跳转至首页
          router.push("/home");
        }
      } else {//登录失败
        isLoginLoading.value = false
        console.log(res.value);
        ElMessage.error(res.value.msg);
      }
    },

    //进入个人中心时的tab切换
    changeTab(tab) {
      this.activeName = tab
    },

    //退出登录
    async logout() {
      //清除token
      commonUtil.removeStore('token')
      //清空状态管理的用户信息状态
      this.userInfo = {};
      this.token = '';
      //更新导航栏状态
      this.nav = 0;
      //提示登出成功
      ElMessage({
        type: 'success',
        message: '退出登录成功'
      })
    },


    //查询用户信息
    async queryUserInfo() {
      const res = await selectByToken();
      if (res.code === 200) {
        console.log(res);
        this.userInfo = res.data
      }
    },
    //完善用户信息
    async submitUserInfo(data, dialogVisible) {
      const res = await updateUserInfo(data);
      if (res.code === 200) {
        ElMessage({
          type: "success",
          message: "修改成功",
        });
        this.queryUserInfo();
        dialogVisible.value = false;
      } else {
        ElMessage({
          type: "error",
          message: "修改失败",
        });
      }
    },

    //查询企业用户信息
    async queryEmployInfo() {
      const res = await selectEmployByToken();
      console.log(res);
      if (res.code === 200) {
        this.userInfo = res.data
      }
    },
    //完善用户信息
    async submitEmployInfo(data, dialogVisible) {
      const res = await updateEmployInfo(data);
      if (res.code === 200) {
        ElMessage({
          type: "success",
          message: "修改成功",
        });
        this.queryEmployInfo();
        dialogVisible.value = false;
      } else {
        ElMessage({
          type: "error",
          message: "修改失败",
        });
      }
    },


    //获取学校列表
    async getSchoolList(schoolName) {
      const res = await getSchools(schoolName)
      if (res.code === 200) {
        this.schoolList = res.data
      }
    },

    //获取专业列表
    async getMajorList(majorName) {
      const res = await getmajors(majorName)
      if (res.code === 200) {
        this.majorList = res.data
      }
    },

    //获取地区列表
    async getProvinceList() {
      const res = await getProvinces()
      if (res.code === 200) {
        this.provinceList = res.data
      }
    },
    //获取职位列表
    async getJobNamesList() {
      const res = await getJobNames()
      console.log(res);
      if (res.code === 200) {
        this.jobList = res.data
      }
    }
  },
});
